﻿using HR.Models;
using System.Net.Mail;
using System.Text;

namespace HR.Config
{
    public class MailConfig
    {
        public bool SendEmail(Mail mails)
        {
            //将发件人邮箱带入MailAddress中初始化
            MailAddress mailAddress = new MailAddress(mails.fromPerson);
            //创建Email的Message对象
            MailMessage mailMessage = new MailMessage();
            //添加发件人
            mailMessage.To.Add("2384264674@qq.com");
            //发件人邮箱
            mailMessage.From = mailAddress;
            //标题
            mailMessage.Subject = mails.mailTitle;
            //编码
            mailMessage.SubjectEncoding = Encoding.UTF8;
            //正文
            mailMessage.Body = mails.mailBody;
            //正文编码
            mailMessage.BodyEncoding = Encoding.Default;
            //邮件优先级
            mailMessage.Priority = MailPriority.High;
            //正文是否是html格式
            mailMessage.IsBodyHtml = false;
            //实例化一个Smtp客户端
            SmtpClient smtp = new SmtpClient();
            //指定SMTP邮件服务器,定义发送为QQ邮箱
            smtp.Host = "smtp.qq.com";
            //将发件人的邮件地址和客户端授权码带入以验证发件人身份
            smtp.Credentials = new System.Net.NetworkCredential(mails.fromPerson, mails.code);
            //邮件发送到SMTP服务器
            smtp.Send(mailMessage);
            return true;
        }
    }
}
